

#ifndef _POWER_CTRL_H_
#define _POWER_CTRL_H_


#include "stdint.h"
#include "arm_math.h"


#define  POWER_ON_CHECK_TIME   2000
#define  POWER_OFF_CHECK_TIME  3000
#define  POWER_REQ_HOLD_TIME   3000

#define  POWER_ON_GAP_TIME     500

#define  POWER_RELAY_DELAY         500
#define  POWER_PRECHARGE_TIMEOUT   5000
#define  POWER_PRECHARGE_TIMEHOLD  1000
#define  POWER_PRECHARGE_DIFF      15.0f


#define  CHARGE_RELAY_ACT_DELAY    50
#define  CHARGE_RECOVER_DELAY      30

#define  DISCHARGE_SOC_THRESHOL    98
#define  CHARGE_SOC_THRESHOLD      96


typedef enum
{
	POWER_ON_KEY,
	POWER_KEY_DIS,
	POWER_ON_HOLD,
	POWER_OFF_REQ,
	POWER_OFF_EXE

}ENUM_POWER_FSM;


typedef enum
{
	IDLE = 0,
	EXE_NOW = 1,
	EXE_CANCEL = 2

}ENUM_POWER_OFF;


typedef enum
{
	RELAY_POWER_OFF,
	RELAY_PRECHARGE,
	RELAY_POWER_ON,
	RELAY_POWER_HOLD,
	RELAY_POWER_FAULT,
	RELAY_PRECHARGE_FAULT

}ENUM_POWER_RELAY;


typedef enum
{
	POWER_OFF = 0,
	POWER_ON = 1,
	POWER_FAULT

}ENUM_POWER_STATUS;



typedef enum
{
	CHARGE_OFF  = 0,
	CHARGE_ON   = 1,
	CHARGE_FULL = 2,
	CHARGE_INT,

}ENUM_CHARGE_STATUS;







typedef struct
{
	ENUM_POWER_FSM FSM;
	
	uint16_t periphepral_start;
	
	uint16_t power_on_cnt;
	uint16_t power_off_cnt;
	uint16_t power_off_req_cnt;
	uint16_t power_off_cnt_down;
	
	ENUM_POWER_OFF power_off_type;
	
	ENUM_POWER_STATUS power12V_device;
	ENUM_POWER_STATUS power12V_nav;
	ENUM_POWER_STATUS power5V_ultra;
	ENUM_POWER_STATUS powerDC_motor;
	ENUM_POWER_STATUS powerDC_charge;
	
	uint16_t relay_delay_cnt;
	
	float32_t DC_voltage;
	float32_t relay_voltage_V;
	
	ENUM_POWER_RELAY  mainRelay_FSM;
	
	ENUM_POWER_STATUS powerDC_service;
	ENUM_POWER_STATUS chargeDC_service;
	
	
	ENUM_POWER_STATUS power12V_status;
	
		

}STR_POWER_CTRL;


typedef struct
{
	ENUM_CHARGE_STATUS FSM;
	ENUM_POWER_RELAY   FSM_realy;
	
	uint8_t bms_charge_status;
	uint8_t bms_bat_soc;
	uint16_t delay_cnt;
	
	uint8_t relay_ctrl;
	uint8_t relay_hold;
	
	float32_t charge_time_s;

}STR_CHARGE_CTRL;















extern STR_POWER_CTRL powerCtrl;


extern STR_CHARGE_CTRL cleanChargeHdl;

extern STR_CHARGE_CTRL serChargeHdl;
extern STR_CHARGE_CTRL chargeCtrl;


extern void set_power_off_type(ENUM_POWER_OFF type);
extern void power_on_off_handle(uint8_t power_key_status, uint16_t run_time_ms);

extern ENUM_POWER_STATUS get_power12V_ultra_status(void);

extern void power_manage_handle(uint16_t run_time_ms);

extern void auto_charge_ctrl(uint16_t run_time_ms);


#endif

